/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.session;

import com.thienloc.omap.entity.Broadcast;
import java.util.List;
import org.slf4j.LoggerFactory;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author GiangLT
 */
@Stateless
public class BroadcastFacade extends AbstractFacade<Broadcast> {

    @PersistenceContext(unitName = "omapPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public BroadcastFacade() {
        super(Broadcast.class);
    }

    public Integer countByStatus(String status, String ipcModule) throws Exception {
        try {
            TypedQuery<Long> q = em.createQuery("SELECT count(b) FROM Broadcast b WHERE b.status = :status AND b.ipcModule = :ipcModule", Long.class);
            q.setParameter("status", status);
            q.setParameter("ipcModule", ipcModule);
            Integer result = q.getSingleResult().intValue();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(BroadcastFacade.class.getName()).error("", ex);
            return null;
        }
    }

    public List<Broadcast> findByStatus(String status, String ipcModule) {
        try {
            TypedQuery<Broadcast> q = em.createNamedQuery("Broadcast.findByStatus", Broadcast.class);
            q.setParameter("status", status);
            q.setParameter("ipcModule", ipcModule);
            List<Broadcast> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(BroadcastFacade.class.getName()).error("", ex);
            return null;
        }
    }

    public Integer deleteByStatus(String status, String ipcModule) {
        try {
            TypedQuery<Broadcast> q = em.createNamedQuery("Broadcast.deleteByStatus", Broadcast.class);
            q.setParameter("status", status);
            q.setParameter("ipcModule", ipcModule);
            Integer result = q.executeUpdate();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(BroadcastFacade.class.getName()).error("", ex);
            return null;
        }
    }

    public List<Broadcast> findByStatus(String status, String ipcModule, int maxResult) {
        try {
            TypedQuery<Broadcast> q = em.createNamedQuery("Broadcast.findByStatus", Broadcast.class);
            q.setParameter("status", status);
            q.setParameter("ipcModule", ipcModule);
            q.setMaxResults(maxResult);
            List<Broadcast> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(BroadcastFacade.class.getName()).error("", ex);
            return null;
        }
    }

    public List<String> findIpcModules() {
        try {
            TypedQuery<String> q = em.createNamedQuery("Broadcast.findIpcModules", String.class);
            List<String> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(BroadcastFacade.class.getName()).error("", ex);
            return null;
        }
    }

    public Broadcast findByMsisdnAndStatus(String msisdn, String status, String ipcModule) {
        try {
            TypedQuery<Broadcast> q = em.createNamedQuery("Broadcast.findByMsisdnAndStatus", Broadcast.class);
            q.setParameter("msisdn", msisdn);
            q.setParameter("status", status);
            q.setParameter("ipcModule", ipcModule);
            Broadcast result = q.getSingleResult();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(BroadcastFacade.class.getName()).error("", ex);
            return null;
        }
    }

    public Broadcast findByMsisdn2Status(String msisdn, String status1, String status2) {
        try {
            TypedQuery<Broadcast> q = em.createNamedQuery("Broadcast.findByMsisdn2Status", Broadcast.class);
            q.setParameter("msisdn", msisdn);
            q.setParameter("status1", status1);
            q.setParameter("status2", status2);
            Broadcast result = q.getSingleResult();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(BroadcastFacade.class.getName()).error("", ex);
            return null;
        }
    }

    public List<Broadcast> findByMsisdn2StatusDestination(String msisdn, String destination, String status1, String status2) {
        try {
            TypedQuery<Broadcast> q = em.createNamedQuery("Broadcast.findByMsisdn2StatusDestination", Broadcast.class);
            q.setParameter("msisdn", msisdn);
            q.setParameter("destination", destination);
            q.setParameter("status1", status1);
            q.setParameter("status2", status2);
            List<Broadcast> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(BroadcastFacade.class.getName()).error("", ex);
            return null;
        }
    }
}
